package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.EduCourseDescription;
import com.atguigu.eduservice.entity.vo.CourseInfoVo;
import com.atguigu.eduservice.entity.vo.CoursePublishVo;
import com.atguigu.eduservice.entity.vo.CourseQuery;
import com.atguigu.eduservice.mapper.EduCourseMapper;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduCourseDescriptionService;
import com.atguigu.eduservice.service.EduCourseService;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.exceptionhandler.GuiliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-06-01
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {
    @Autowired
    private EduCourseDescriptionService courseDescriptionService;
    @Autowired
    private EduVideoService videoService;
    @Autowired
    private EduChapterService chapterService;

    //添加课程信息的方法
    @Override
    public void saveCourseInfo(CourseInfoVo courseInfoVo) {
        //向课程表添加课程基本信息

        EduCourse eduCourse = new EduCourse();
        //CourseInfoVo对象转换为eduCourse对象
        BeanUtils.copyProperties(courseInfoVo, eduCourse);
        int insert = baseMapper.insert(eduCourse);
        if (insert <= 0) {
            throw new GuiliException(20001, "添加课程失败");
        }
        //获取添加之后课程id
        String cid = eduCourse.getId();
        //向课程简介表添加课程简介

        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        //CourseInfoVo对象转换为eduCourse对象
        eduCourseDescription.setDescription(courseInfoVo.getDescription());
        eduCourseDescription.setId(cid);
        courseDescriptionService.save(eduCourseDescription);
    }

    //根据课程id获取课程基本信息
    @Override
    public CourseInfoVo getCourseInfo(String courseId) {
        //查询课程表
        EduCourse eduCourse = baseMapper.selectById(courseId);//返回eduCourse课程对象
        //新建一个courseInfoVo对象
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        //将查询课程表eduCourse信息封装进courseInfoVo
        BeanUtils.copyProperties(eduCourse, courseInfoVo);

        //查询描述表
        EduCourseDescription courseDescription = courseDescriptionService.getById(courseId);
        //将描述表courseDescription信息封装进courseInfoVo
        courseInfoVo.setDescription(courseDescription.getDescription());
        //返回courseInfoVo
        return courseInfoVo;
    }

    //修改课程信息
    @Override
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo, eduCourse);
        //修改课程表
        int update = baseMapper.updateById(eduCourse);
        if (update == 0) {
            throw new GuiliException(20001, "修改课程信息失败");
        }
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        BeanUtils.copyProperties(courseInfoVo, eduCourseDescription);
        //修改描述表
        courseDescriptionService.updateById(eduCourseDescription);
    }

    //根据课程ID查询课程确认信息
    @Override
    public CoursePublishVo getPublishInfo(String id) {
        CoursePublishVo publishInfo = baseMapper.getPublishInfo(id);
        return publishInfo;
    }

    //课程最终发布，修改课程状态为Normal
    @Override
    public Boolean publishCourse(String id) {
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");
        int i = baseMapper.updateById(eduCourse);
        return i > 0;
    }

    //课程列表，带条件分页
    @Override
    public Page<EduCourse> pageCourseCondition(int current, int limit, CourseQuery courseQuery) {
        Page<EduCourse> page = new Page<>(current, limit);
        QueryWrapper<EduCourse> courseQueryWrapper = new QueryWrapper<>();

        if (!StringUtils.isEmpty(courseQuery.getTitle())) {
            courseQueryWrapper.eq("title", courseQuery.getTitle());
        }

        if (!StringUtils.isEmpty(courseQuery.getStatus())) {
            courseQueryWrapper.eq("status", courseQuery.getStatus());
        }
        if (!StringUtils.isEmpty(courseQuery.getBegin())) {
            courseQueryWrapper.eq("gmt_create", courseQuery.getBegin());
        }
        if (!StringUtils.isEmpty(courseQuery.getEnd())) {
            courseQueryWrapper.eq("gmt_create", courseQuery.getEnd());
        }
        //排序
        courseQueryWrapper.orderByDesc("gmt_create");
        baseMapper.selectPage(page, courseQueryWrapper);
        return page;
    }

    //根据课程id删除课程
    @Override
    public void deleteCourse(String courseId) {
        //根据课程id删除小节
        videoService.removeVideoByCourseId(courseId);
        //根据课程id删除章节
        chapterService.removeChapterByCourseId(courseId);
        //根据课程id删除课程描述
        courseDescriptionService.removeById(courseId);
        //根据课程id删除课程
        int i = baseMapper.deleteById(courseId);
        if (i == 0) {
            throw new GuiliException(20001, "删除课程失败");
        }
    }
}
